package cn.t.modules.scheduler.rest;

import cn.t.modules.scheduler.common.data.ErrorCode;
import cn.t.modules.scheduler.common.data.R;
import cn.t.modules.scheduler.common.exeptions.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * @author 陶敏麒
 * @date 2022/12/15 14:53
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 参数校验
     * @param e 参数校验失败异常
     * @return 响应结果
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public R<Object> paramValidFailedHandler(MethodArgumentNotValidException e) {
        BindingResult result = e.getBindingResult();
        final List<FieldError> fieldErrors = result.getFieldErrors();
        StringBuilder builder = new StringBuilder();
        for (FieldError error : fieldErrors) {
            builder.append(error.getDefaultMessage()).append("\n");
        }
        log.error("参数不合法: {}", builder);
        return R.error(ErrorCode.PARAM_INVALID.getCode(), builder.toString());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    //@ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> handHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
        log.warn("请求方法不支持，{}", e.getMessage());
        return R.error(ErrorCode.INVALID_OPERATE.getCode(), "请求方法不支持");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    //@ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> handHttpMessageNotReadableException(HttpMessageNotReadableException e){
        log.warn("参数读取异常, {}", e.getMessage());
        return R.error(ErrorCode.INVALID_OPERATE.getCode(), "参数读取异常");
    }


    @ExceptionHandler(HttpMessageConversionException.class)
    public R<Object> httpMessageConversionException(HttpMessageConversionException e){
        log.error("参数映射异常, {}", e.getMessage());
        return R.error(ErrorCode.INVALID_OPERATE.getCode(), "参数映射异常");
    }

    @ExceptionHandler({BizException.class})
    public R<Object> bizException(BizException bizException){
        log.error("捕获到业务异常,msg=[{}]", bizException.getMessage());
        return R.error(ErrorCode.BIZ_ERROR.getCode(), bizException.getMessage());
    }

    @ExceptionHandler({Exception.class})
    public R<Object> unKnowException(Exception e){
        log.error("未知异常", e);
        return R.error(ErrorCode.SYS_ERROR.getCode(), "系统异常");
    }
}
